<?php
/**
 * Member Controller
 * 
 * @author Nguyen Hai Ha
 * @version 1.0
 */

class Administrator_MemberController extends Zend_Controller_Action {
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	protected $_errors;
	
	public function init() {
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "administrator/member";
		
		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );
		
		$this->_utilities = new Utilities ( );
		$this->view->assign ( "utilities", $this->_utilities );
		
		$this->_errors = array ();
	}
	
	public function indexAction() {
		$this->_redirect ( $this->_contextPath . "/list-pending" );
	}
	
	public function addAction() {
		if ($this->_request->isPost ()) {
			// Save member.
			$f = new Zend_Filter_StripTags ( );
			
			$usr_mobile = $f->filter ( $this->_request->getPost ( "usr_mobile" ) );
			$usr_password = $f->filter ( $this->_request->getPost ( "usr_password" ) );
			$usr_fullname = $f->filter ( $this->_request->getPost ( "usr_fullname" ) );
			$usr_birthday = $f->filter ( $this->_request->getPost ( "usr_birthday" ) );
			$usr_email = $f->filter ( $this->_request->getPost ( "usr_email" ) );
			$usr_coin_amount = ( int ) $f->filter ( $this->_request->getPost ( "usr_coin_amount" ) );
			$usr_role = $f->filter ( $this->_request->getPost ( "usr_role" ) );
			$usr_activated = ( int ) $f->filter ( $this->_request->getPost ( "usr_activated" ) );
			if ($usr_activated)
				$usr_activated_date = date ( 'Y-m-d H:m:s' );
			else
				$usr_activated_date = null;
				
			if (strlen ( $usr_birthday ))
				$usr_birthday = $this->_utilities->formatDateToDatabase ( $usr_birthday );
				
			$account = array ('usr_mobile' => $usr_mobile, 'usr_password' => md5 ( $usr_password ), 'usr_fullname' => $usr_fullname, 'usr_birthday' => $usr_birthday, 'usr_email' => $usr_email, 'usr_activated' => $usr_activated, 'usr_activated_date' => $usr_activated_date, 'usr_role' => $usr_role, 'usr_created_date' => date ( 'Y-m-d H:m:s' ), 'usr_coin_amount' => $usr_coin_amount );
			
			$userObject = new UserModel ( );
			
			// Validate.
			if (! strlen ( $usr_mobile ))
				$this->_errors ['mobile_null'] = "Ban phai nhap so dien thoai.";
			else {
				if ($userObject->checkUserExist ( $usr_mobile ))
					$this->_errors ['mobile_exist'] = "So dien thoai nay da ton tai.";
				if (! $this->_utilities->check_mobile ( $usr_mobile ))
					$this->_errors ['mobile_incorrect'] = "So dien thoai ban nhap khong hop le.";
			}
			if (! strlen ( $usr_password ))
				$this->_errors ['password_null'] = "Ban phai nhap password.";
			if (! strlen ( $usr_fullname ))
				$this->_errors ['fullname_null'] = "Ban phai nhap ho va ten.";
			if (strlen ( $usr_email ))
				if (!$this->_utilities->checkemail ( $usr_email ))
					$this->_errors ['email_incorrect'] = "Dia chi email khong dung.";
			if ($usr_role == Constant::GUEST)
				$this->_errors['role_null'] = "Ban phai chon role cho member.";
			
			if (count ( $this->_errors )) {
				$account ['usr_birthday'] = $this->_utilities->formatdate ( $account ['usr_birthday'] );
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "account", $account );
				$this->render ( "add-member" );
				return;
			}
			
			$userObject->insertUser ( $account );
			
			$this->_redirect ( $this->_contextPath . "/successfull" );
			return;
		}
		
		// Hien thi man hinh create member.
		$account = array ('usr_mobile' => '', 'usr_password' => '', 'usr_fullname' => '', 'usr_birthday' => '', 'usr_email' => '', 'usr_activated' => '', 'usr_activated_date' => '', 'usr_role' => '', 'usr_created_date' => '', 'usr_coin_amount' => '' );
		$this->view->assign ( "account", $account );
		$this->render ( "add-member" );
	}
	
	public function editPendingAction() {
		if ($this->_request->isPost ()) {
			// Su kien save account.	
			$page = ( int ) $this->_request->getPost ( "page" );
			$user_id = ( int ) $this->_request->getPost ( "user_id" );
			
			$f = new Zend_Filter_StripTags ( );
			
			$usr_mobile = $f->filter ( $this->_request->getPost ( "usr_mobile" ) );
			$usr_fullname = $f->filter ( $this->_request->getPost ( "usr_fullname" ) );
			$usr_birthday = $f->filter ( $this->_request->getPost ( "usr_birthday" ) );
			$usr_email = $f->filter ( $this->_request->getPost ( "usr_email" ) );
			$usr_coin_amount = ( int ) $f->filter ( $this->_request->getPost ( "usr_coin_amount" ) );
			
			$usr_activated = ( int ) $f->filter ( $this->_request->getPost ( "usr_activated" ) );
			if ($usr_activated)
				$usr_activated_date = date ( 'Y-m-d H:m:s' );
			else
				$usr_activated_date = null;
				
			if (strlen ( $usr_birthday ))
				$usr_birthday = $this->_utilities->formatDateToDatabase ( $usr_birthday );
			
			$account = array ('usr_id' => $user_id, 'usr_mobile' => $usr_mobile, 'usr_fullname' => $usr_fullname, 'usr_birthday' => $usr_birthday, 'usr_email' => $usr_email, 'usr_activated' => $usr_activated, 'usr_activated_date' => $usr_activated_date, 'usr_coin_amount' => $usr_coin_amount );
			
			// Validate.
			if (! strlen ( $usr_fullname ))
				$this->_errors ['fullname_null'] = "Ban phai nhap ho va ten.";
			if (strlen ( $usr_email ))
				if (! $this->_utilities->checkemail ( $usr_email ))
					$this->_errors ['email_incorrect'] = "Dia chi email khong dung.";
			
			if (count ( $this->_errors )) {
				$account ['usr_birthday'] = $this->_utilities->formatdate ( $account ['usr_birthday'] );
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "user", $account );
				$this->view->assign ( "page", $page );
				$this->render ( "edit-pending" );
				return;
			}
			
			$userModel = new UserModel ( );
			if ($user_id != 1) {
				$userModel->update ( $account, $user_id );
			}
			
			$this->_redirect ( $this->_contextPath . "/list-pending/page/" . $page );
		} else {
			// Su kien edit account.
			$user_id = ( int ) $this->_request->getParam ( "user_id" );
			$page = ( int ) $this->_request->getParam ( "page" );
			$userModel = new UserModel ( );
			
			$user = $userModel->getUserById ( $user_id );
			$this->view->assign ( "user", $user );
			$this->view->assign ( "page", $page );
			
			$this->render ( "edit-pending" );
		}
	}
	
	public function editActivatedAction() {
		if ($this->_request->isPost ()) {
			// Su kien save account.	
			$page = ( int ) $this->_request->getPost ( "page" );
			$user_id = ( int ) $this->_request->getPost ( "user_id" );
			
			$f = new Zend_Filter_StripTags ( );
			
			$usr_mobile = $f->filter ( $this->_request->getPost ( "usr_mobile" ) );
			$usr_fullname = $f->filter ( $this->_request->getPost ( "usr_fullname" ) );
			$usr_birthday = $f->filter ( $this->_request->getPost ( "usr_birthday" ) );
			$usr_email = $f->filter ( $this->_request->getPost ( "usr_email" ) );
			$usr_coin_amount = ( int ) $f->filter ( $this->_request->getPost ( "usr_coin_amount" ) );
			
			$usr_activated = ( int ) $f->filter ( $this->_request->getPost ( "usr_activated" ) );
			if (! $usr_activated)
				$usr_activated_date = null;
				
			if (strlen ( $usr_birthday ))
				$usr_birthday = $this->_utilities->formatDateToDatabase ( $usr_birthday );
			
			$account = array ('usr_id' => $user_id, 'usr_mobile' => $usr_mobile, 'usr_fullname' => $usr_fullname, 'usr_birthday' => $usr_birthday, 'usr_email' => $usr_email, 'usr_activated' => $usr_activated, 'usr_activated_date' => $usr_activated_date, 'usr_coin_amount' => $usr_coin_amount );
			
			// Validate.
			if (! strlen ( $usr_fullname ))
				$this->_errors ['fullname_null'] = "Ban phai nhap ho va ten.";
			if (strlen ( $usr_email ))
				if (! $this->_utilities->checkemail ( $usr_email ))
					$this->_errors ['email_incorrect'] = "Dia chi email khong dung.";
			
			if (count ( $this->_errors )) {
				$account ['usr_birthday'] = $this->_utilities->formatdate ( $account ['usr_birthday'] );
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "user", $account );
				$this->view->assign ( "page", $page );
				$this->render ( "edit-activated" );
				return;
			}
			
			$userModel = new UserModel ( );
			if ($user_id != 1) {
				$userModel->update ( $account, $user_id );
			}
			
			$this->_redirect ( $this->_contextPath . "/list-activated/page/" . $page );
		} else {
			// Su kien edit account.
			$user_id = ( int ) $this->_request->getParam ( "user_id" );
			$page = ( int ) $this->_request->getParam ( "page" );
			$userModel = new UserModel ( );
			
			$user = $userModel->getUserById ( $user_id );
			$this->view->assign ( "user", $user );
			$this->view->assign ( "page", $page );
			
			$this->render ( "edit-activated" );
		}
	}
	
	public function successfullAction() {
	}
	
	public function activatedAction() {
		$user_id = ( int ) $this->_request->getParam ( "user_id" );
		$page = ( int ) $this->_request->getParam ( "page" );
		$usr_activated_date = date ( 'Y-m-d H:m:s' );
		$activated = array ('usr_activated' => 1, 'usr_activated_date'=>$usr_activated_date );
		$userModel = new UserModel ( );
		$userModel->update ( $activated, $user_id );
		
		$this->_redirect ( $this->_contextPath . "/list-pending/page/" . $page );
	}
	
	public function deActivatedAction() {
		$user_id = ( int ) $this->_request->getParam ( "user_id" );
		$page = ( int ) $this->_request->getParam ( "page" );
		$usr_activated_date = null;
		$activated = array ('usr_activated' => 0, 'usr_activated_date'=>$usr_activated_date );
		$userModel = new UserModel ( );
		$userModel->update ( $activated, $user_id );
		
		$this->_redirect ( $this->_contextPath . "/list-activated/page/" . $page );
	}
	
	public function deletePendingMemberAction() {
		$user_id = ( int ) $this->_request->getParam ( "user_id" );
		$page = ( int ) $this->_request->getParam ( "page" );
		$userModel = new UserModel ( );
		$userModel->delete ( $user_id );
		
		$this->_redirect ( $this->_contextPath . "/list-pending/page/" . $page );
	}
	
	public function deleteActivatedMemberAction() {
		$user_id = ( int ) $this->_request->getParam ( "user_id" );
		$page = ( int ) $this->_request->getParam ( "page" );
		$userModel = new UserModel ( );
		$userModel->delete ( $user_id );
		
		$this->_redirect ( $this->_contextPath . "/list-activated/page/" . $page );
	}
	
	public function searchAction() {
	}
	
	public function listPendingAction() {
		$userModel = new UserModel ( );
		$activated = 0;
		
		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::MEMBER_LIST_ITEM;
		$totalRecords = $userModel->getTotalRecords ( $activated );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
			$currentPages = 1;
			//Set url for paging.	
		//Render paging for page.	
		//Render paging title for page.	
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/list-pending";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.
		

		$listPendingUsers = $userModel->getAllUser ( $activated, $currentPages, $itemPerPage );
		$this->view->assign ( "totalRecords", $totalRecords );
		$this->view->assign ( "users", $listPendingUsers );
		$this->render ( "list-pending" );
	}
	
	public function listActivatedAction() {
		$userModel = new UserModel ( );
		$activated = 1;
		
		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::MEMBER_LIST_ITEM;
		$totalRecords = $userModel->getTotalRecords ( $activated );
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
			$currentPages = 1;
			//Set url for paging.	
		//Render paging for page.	
		//Render paging title for page.	
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/list-activated";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.
		

		$listPendingUsers = $userModel->getAllUser ( $activated, $currentPages, $itemPerPage );
		$this->view->assign ( "totalRecords", $totalRecords );
		$this->view->assign ( "users", $listPendingUsers );
		$this->render ( "list-activated" );
	}
	
	public function listAdministratorAction() {
		$userModel = new UserModel ( );
		
		//Begin: render script for paging.
		$paging = new Paging ( );
		//Get item on page.
		//Get total page.
		//Get current page.
		$itemPerPage = Constant::MEMBER_LIST_ITEM;
		$totalRecords = $userModel->getTotalAdministratorRecords();
		$currentPages = ( int ) $this->_request->getParam ( 'page' );
		if ($currentPages == 0)
			$currentPages = 1;
			//Set url for paging.	
		//Render paging for page.	
		//Render paging title for page.	
		$url = $this->_baseUrl . "/" . $this->_contextPath . "/list-administrator";
		$page = $paging->doPaging ( $totalRecords, $url, $currentPages, $itemPerPage );
		$page_title = $paging->doPageSeparator ( $totalRecords, $currentPages, $itemPerPage );
		//Set for view.
		$this->view->assign ( "page", $page );
		$this->view->assign ( "page_title", $page_title );
		$this->view->assign ( "currentPage", $currentPages );
		//End: render script for paging.
		

		$listPendingUsers = $userModel->getAllAdministrator( $currentPages, $itemPerPage );
		$this->view->assign ( "totalRecords", $totalRecords );
		$this->view->assign ( "users", $listPendingUsers );
		$this->render ( "list-administrator" );
	}

}//End class.